package com.myfojing.foshuoliaozhibingjing;

import android.content.res.Resources;
import android.util.Log;

import com.github.promeg.pinyinhelper.Pinyin;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Sutra {
    private String[] sutra;
    private String[] zhuyin;
    private int count;
    private int length;
    private int idx;
    private Kind kind;

    Sutra(int id_sutra, int id_zhuyin, int count, Kind kind, Resources res) {
        this.sutra = res.getString(id_sutra).split("\n");
        this.zhuyin = res.getString(id_zhuyin).split("\n");
        this.count = count;
        this.kind = kind;

        this.idx = 0;
        this.length = sutra.length;
    }

    String[] get_strings() {
        String jingwen;
        String zhuyin;
        String jingwen_speak;
        // color: #0C090A
        String html = "<!DOCTYPE HTML>" +
                "<html>" +
                "<head>" +
                "<meta charset=\"UTF-8\">" +
                "<style>" +
                "p.jw {font-size:32px;color:rgb(59,156,156)}" +
                "p.zy_0 {font-size:32px;color:rgb(56, 172, 236)}" +
                "p.zy_1 {font-size:32px;color:rgb(237, 89, 78)}" +
                "p.zy_2 {font-size:32px;color:rgb(205, 127, 50)}" +
                "</style>" +
                "</head>" +
                "<body>" +
                "%s" +
                "</body>" +
                "</html>";

        jingwen = this.sutra[idx].trim();
        jingwen_speak = jingwen;
        // 删掉jingwen中的标记
        Pattern pattern = Pattern.compile("\\[.*?\\]");
        Matcher matcher = pattern.matcher(jingwen);
        if (matcher.find()) {
            jingwen = matcher.replaceAll("");
        }

        zhuyin = this.zhuyin[idx].trim();

        // 分句长度
        //int clause_length = jingwen.length();

        char[] jingwen_c = jingwen.toCharArray();
        String[] zhuyin_c = zhuyin.split(" ");
        String prefix = "<p class=\"jw\">";
        String suffix = "</ruby></p>";
        String strDisplay = "<ruby>";
        int j = 0;
        for (int i = 0; i < jingwen_c.length; i++) {
            if (jingwen_c[i] == '，' || jingwen_c[i] == '。' || jingwen_c[i] == '！' || jingwen_c[i] == '；'
                    || jingwen_c[i] == '？' || jingwen_c[i] == '：' || jingwen_c[i] == '、') {
                strDisplay += jingwen_c[i];
                strDisplay += "<rt> </rt>";
                continue;
            }

            strDisplay += jingwen_c[i] + "<rt>" + zhuyin_c[j] + "</rt>";
            j++;
        }
        //strDisplay += "</ruby>";
        strDisplay = prefix + strDisplay + suffix;
        strDisplay = String.format(Locale.CHINESE, html, strDisplay);

        // return value
        String[] t = new String[2];
        t[0] = jingwen_speak;
        t[1] = strDisplay;
        return t;
    }


    private String get_strings(int idx) {
        String jingwen = "";
        String zhuyin = "";

        jingwen = this.sutra[idx].trim();
        zhuyin = this.zhuyin[idx].trim();

        Pattern pattern = Pattern.compile("\\[.*?\\]");
        Matcher matcher = pattern.matcher(jingwen);
        if (matcher.find()) {
            jingwen = matcher.replaceAll("");
        }

        char[] jingwen_c = jingwen.toCharArray();
        String[] zhuyin_c = zhuyin.split(" ");

        String strDisplay = "";
        int j = 0;
        for (int i = 0; i < jingwen_c.length; i++) {
            if (jingwen_c[i] == '，' || jingwen_c[i] == '。' || jingwen_c[i] == '！' || jingwen_c[i] == '；'
                    || jingwen_c[i] == '？' || jingwen_c[i] == '：' || jingwen_c[i] == '、') {
                strDisplay += jingwen_c[i];
                strDisplay += "<rt> </rt>";
                if (jingwen_c[i] == '。' || jingwen_c[i] == '！' || jingwen_c[i] == '？' || jingwen_c[i] == '；') {
                    strDisplay += "</ruby><br/>\n<ruby>";
                }
                continue;
            }

            strDisplay += jingwen_c[i] + "<rt>" + zhuyin_c[j] + "</rt>";
            j++;
        }
        return strDisplay;
    }

    boolean hasNext() {
        return idx < length;
    }

    void next() {
        idx++;
        if (idx >= length) {
            idx = 0;
            count--;
        }
    }

    boolean finished() {
        return count <= 0;
    }

    void replace(String str) {
        // 替换名字，拼音
        if (str == null) return;
        String str_py = Pinyin.toPinyin(str, " ");
        for (int i = 0; i < sutra.length; i++) {
            sutra[i] = sutra[i].replace("某", str);
            zhuyin[i] = zhuyin[i].replace("mǒu", str_py.toLowerCase());
        }

    }

    void makeHTML() {
        int idx = 0;
        String html = "";
        String r;
        for (int i = 0; i < sutra.length; i++) {
            try {
                String s = get_strings(idx);
                idx++;
                html += s;
            } catch (Exception e) {
                e.printStackTrace();
                Log.e("我的佛经", Integer.toString(idx));
            }
        }
        r = "<ruby>" + html + "</ruby>";
    }

    String getCount() {
        return Integer.toString(count);
    }

    Kind getKind() {
        return kind;
    }
// end
}
